package com.tledu.production.service.system.impl;

import com.tledu.production.core.exception.CustomException;
import com.tledu.production.mapper.system.SysUserMapper;
import com.tledu.production.mapper.system.SysUserRoleMapper;
import com.tledu.production.model.system.SysUser;
import com.tledu.production.model.system.SysUserRole;
import com.tledu.production.service.system.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统管理-用户表
 *
 * @author Antg
 * @date 2021/8/15  21:40
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl implements ISysUserService {
    private SysUserMapper sysUserMapper;
    private SysUserRoleMapper userRoleMapper;
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.findAll();
    }

    @Override
    public SysUser findById(String id) {
        return sysUserMapper.findById(id);
    }

    @Override
    public List<SysUser> search(String type, String searchText) {
        return sysUserMapper.searchByType(type, searchText);
    }

    @Override
    public int lock(String locked, String id, SysUser loginUser) {
        //不可以锁定自己
        if (loginUser.getId().equals(id)) {
            throw new CustomException("不可以锁定当前登录的用户", 500);
        }
        return sysUserMapper.lock(locked, id);
    }


    @Override
    public SysUser login(String username, String password) {
        SysUser sysUser = sysUserMapper.findByUserName(username);
        if (sysUser == null) {
            throw new CustomException("用户不存在", 500);
        }
        if (!password.equals(sysUser.getPassword())) {
            throw new CustomException("密码错误", 500);
        }
        return sysUser;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(SysUser sysUser) {
        //添加时,用户名不可以重复
        if (userUniq(sysUser.getUsername())) {
            throw new CustomException("该用户名已经被使用", 500);
        }

        try {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setId(Long.toString(System.currentTimeMillis()));
            sysUserRole.setSysUserId(sysUser.getId());
            sysUserRole.setSysRoleId(sysUser.getRoleId());
            userRoleMapper.add(sysUserRole);
            return sysUserMapper.addSysUser(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(SysUser sysUser) {
        //编辑时,用户名不可以重复
        if (userUniq(sysUser.getUsername())) {
            throw new CustomException("该用户名已经被使用", 500);
        }

        try {
            //编辑用户的时候将用户角色关联表修改了
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setId(sysUserMapper.findById(sysUser.getId()).getUrId());
            sysUserRole.setSysUserId(sysUser.getId());
            sysUserRole.setSysRoleId(sysUser.getRoleId());
            sysUserRoleMapper.edit(sysUserRole);
            return sysUserMapper.editSysUser(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDel(String[] ids, SysUser loginUser) {
        //不可以删除自己
        for (int i = 0; i < ids.length; i++) {
            if (ids[i].equals(loginUser.getId())) {
                throw new CustomException("不可以删除当前登录的用户", 500);
            }
        }
        //用户最少保留一个,不可以删除为空(防止多用户操作互删的情况导致登录不上系统)
        /*
         * 这里依然存在多线程问题,可以用锁解决,这里就先不解决了
         * */

        int sysUserNum = sysUserMapper.countUnlockedSysUser();
        if (sysUserNum == 1) {
            throw new CustomException("至少保留一个管理员", 500);
        }


        //删除用户的时候将对应关联表中的数据删除(避免脏数据)
        try {
            sysUserRoleMapper.delUserRoleByUserId(ids);
            return sysUserMapper.batchDelSysUser(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @param username 用户名
     * @return 重复返回true否则返回false
     * @author Antg
     * @date 2021/8/22   17:50
     */
    private boolean userUniq(String username) {
        SysUser byUserName = sysUserMapper.findByUserName(username);
        if (byUserName != null) {
            return true;
        } else {
            return false;
        }
    }


}
